package net.pimpas.web.jsf.renderers.radio.ajax;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UISelectOne;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;

import net.pimpas.web.jsf.renderers.AjaxRendererBase;
import net.pimpas.web.jsf.renderers.RendererBase;
import net.pimpas.web.jsf.util.ComponentUtils;

import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils;

public class AjaxRadioRenderer extends RendererBase implements AjaxRendererBase {
	
	public static final String JAVASCRIPT_HANDLER	= "pimpas.AJAX.Populate.Radio";

	public String getJavascriptHandler() {
		return JAVASCRIPT_HANDLER;
	}
	
	@Override
	public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
		if(!component.isRendered()) return;
		if(!ComponentUtils.isCorrectComponentType(component, UISelectOne.class)) return;
		/**
		 * extracted from myfaces-1.1.5-SNAPSHOT
		 */
		
		UISelectOne selectOne = (UISelectOne) component;
		
        Converter converter;
        List selectItemList = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getSelectItemList(selectOne);
        converter = HtmlRendererUtils.findUIOutputConverterFailSafe(context, selectOne);

        Object currentValue = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getObjectValue(selectOne);

        currentValue = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(context, selectOne, converter, currentValue);

        for (Iterator it = selectItemList.iterator(); it.hasNext(); ) {
            SelectItem selectItem = (SelectItem)it.next();

            renderGroupOrItemRadio(context, selectOne,
                                   selectItem, currentValue,
                                   converter);
        }

	}
	

    protected void renderGroupOrItemRadio(FacesContext context,
                                          UIComponent component, SelectItem selectItem,
                                          Object currentValue,
                                          Converter converter) throws IOException{
        boolean isSelectItemGroup = (selectItem instanceof SelectItemGroup);

        UISelectOne selectOne = (UISelectOne)component;

        if (isSelectItemGroup) {
            SelectItemGroup group = (SelectItemGroup) selectItem;
            SelectItem[] selectItems = group.getSelectItems();
            for (int i=0; i<selectItems.length; i++)
                renderGroupOrItemRadio(context, selectOne, selectItems[i], currentValue, converter);
        } else {
	        String itemStrValue = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(context, selectOne, converter, selectItem.getValue());
	        boolean itemChecked = itemStrValue.equals(currentValue);
	        renderRadio(context, selectOne, itemStrValue, itemChecked);
        }
    }

    protected void renderRadio(FacesContext context,
                               UIInput component,
                               String value,
                               boolean checked)
            throws IOException {
    	
        ResponseWriter writer = context.getResponseWriter();

        if (checked) {
        	writer.startElement("value", component);
        	writer.write(value);
        	writer.endElement("value");
        }
    }
}